package com.winit.openapi.gt.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.oms.spi.gt.CustomerPackageService;
import com.winit.oms.spi.gt.command.PrintFbaPackageLabelCommand;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.PDFBox4MergePDF;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单 箱单信息
 * 
 * @version <pre>
 * Author     Version       Date        Changes
 * dinghua.an   1.0         2015年11月10日     Created
 *
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/gt/fbaPackage")
public class GlobalTransferPackageController extends BaseController {

    @Resource
    private CustomerPackageService customerPackageService;

    /**
     * 批量打印包裹标签
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/print")
    public String printPackageLabel(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        PrintFbaPackageLabelCommand command = new PrintFbaPackageLabelCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "winitOrderNo", "winitOrderNo");
            String printSize = json.getString("printSize");
            if (StringUtils.isEmpty(printSize)) {
                printSize = "10x6";
            }
            command.setPrintNum(json.getInteger("printNum"));
            command.setWinitOrderNo(json.getString("winitOrderNo"));
            command.setPrintSize(printSize);
        } else {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, "data");
        }
        command.setCtx(CommandContext.getContext());
        List<String> bytes = customerPackageService.printPackageLabel(command);
        PDFBox4MergePDF tool = new PDFBox4MergePDF();
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            List<byte[]> byteList = tool.decryptBASE64(bytes);
            tool.merge(byteList, out);
        } catch (IOException e) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        Map<String, Object> dataMap = new HashMap<String, Object>();
        String mergeByte = tool.base64Encode(out.toByteArray());

        if (mergeByte != null && mergeByte.length() > 0) {
            dataMap.put("files", mergeByte);
        }
        responseMsg.setData(dataMap);
        return SUCCESS;
    }
}
